/**
 * Copyright 2018
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package io.renren.modules.aic.controller;

import io.renren.common.utils.DateUtils;
import io.renren.common.utils.ProjectConstant;
import io.renren.common.utils.R;
import io.renren.modules.aic.entity.CtocTransactionEntity;
import io.renren.modules.aic.entity.MemberEntity;
import io.renren.modules.aic.entity.MemberMachineEntity;
import io.renren.modules.aic.service.CtocTransactionService;
import io.renren.modules.aic.service.MemberMachineService;
import io.renren.modules.aic.service.MemberService;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;

/**
 * 主页
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2016年11月10日 下午1:15:31
 */
@Controller
@RequestMapping("aic/index")
public class MainController {

	@Autowired
	MemberService memberService;

	@Autowired
	MemberMachineService memberMachineService;

	@Autowired
	CtocTransactionService ctocTransactionService;

	/**
	 * 首页统计
	 */
	@ResponseBody
	@RequestMapping(value = "/main")
	public R main() {
		Date startOfDay = DateUtils.getStartOfDay(new Date());
		// 会员总数
		int members = memberService.selectCount(new EntityWrapper<MemberEntity>());
		// 今日注册会员数
		int todayMembers = memberService.selectCount(new EntityWrapper<MemberEntity>().ge("create_Time", startOfDay));
		// 虚拟币数量
		double totalBalance = memberService.totalBalance();
		// 运行中的矿机
		int runningMachine = memberMachineService.selectCount(new EntityWrapper<MemberMachineEntity>().eq("status", ProjectConstant.MACHINE_RUNSTATUS_RUNNING));
		// 今日购买的矿机
		int todayMachine = memberMachineService.selectCount(new EntityWrapper<MemberMachineEntity>().eq("status", ProjectConstant.MACHINE_RUNSTATUS_RUNNING).ge("create_time",
				startOfDay));
		// 今日成交笔数
		int orders = ctocTransactionService.selectCount(new EntityWrapper<CtocTransactionEntity>().eq("status", ProjectConstant.CTOC_TRANS_STATUS_CONFIRMED).ge("create_Time",
				startOfDay));
		// 今日成交总额
		double totalAmount = ctocTransactionService.totalAmount(startOfDay);
		// 今日成交总币数
		double totalCoinAmount = ctocTransactionService.totalCoinAmount(startOfDay);

		return R.ok().put("members", members).put("todayMembers", todayMembers).put("totalBalance", totalBalance).put("runningMachine", runningMachine)
				.put("todayMachine", todayMachine).put("orders", orders).put("totalAmount", totalAmount).put("totalCoinAmount", totalCoinAmount);
	}
}
